////////////////////////////////////////////////////////////////////////////////////////////////
//	File			:		"CDebugConsole.h"
//
//	Author			:		Victor J Bonilla	
////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef CDEBUGCONSOLE_H_
#define CDEBUGCONSOLE_H_

// Include files
#include <windows.h>
#include <process.h>
#include <iostream>
using std::iostream;

// Class declaration
class CDebugConsole : public iostream
{

#define MAX_STRING_LENGTH 512

private:
	CDebugConsole();
	CDebugConsole(const CDebugConsole&);
	CDebugConsole& operator = (const CDebugConsole&);
	~CDebugConsole();

	// Handles 
	HANDLE	m_hConsole;
	HANDLE	m_hInputHandle;
	HANDLE	ThreadHandle;

	//Boolean to let the thread know is time to go
	volatile bool	m_bTimeToLeave;	

	//Critical Section
	CRITICAL_SECTION* LockSection;

	//Function to do lock and Unlock the thread
	void LockIt()
	{
		EnterCriticalSection(LockSection);
	}
	void UnLockIt()
	{
		LeaveCriticalSection(LockSection);
	}

	// Static instance
	static CDebugConsole* m_pInstance;


public:

	//Accessor
	bool ShutDownTime()
	{
		bool bTemp;

		LockIt();
			bTemp = m_bTimeToLeave;
		UnLockIt();

		return bTemp;
	}
	
	//Mutator
	void SetShutDownTime(bool Rdy)
	{
		LockIt();
		m_bTimeToLeave = Rdy;
		UnLockIt();
	}


	static CDebugConsole* GetInstance();

	void Initialize();
	void ShutDown();
	void Display(const char* szString)
	{
		DWORD dCharWrote;
		WriteConsole(m_hConsole, szString, strlen(szString), &dCharWrote, 0);
	}
	
	void Read()
	{
		char szBuffer[MAX_STRING_LENGTH];
		unsigned long n = MAX_STRING_LENGTH;
		LockIt();
		ReadConsole(m_hInputHandle, szBuffer, MAX_STRING_LENGTH, &n, 0);
		UnLockIt();
	}
};

#endif	